package com.pai4j.pgc.service.relation;
import com.pai4j.common.enums.MessageTypeEnum;
import com.pai4j.common.enums.SubjectRelationTypeEnum;
import com.pai4j.common.enums.messagequeue.MessageBroadChannelEnum;
import com.pai4j.common.service.messagequeue.producer.MessageQueueProducer;
import com.pai4j.domain.vo.request.mess.MessageNotifyVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;


@Service
public class User2SubjectSubjectRelationService extends AbstractSubjectRelationService {

    @Resource
    private MessageQueueProducer messageQueueProducer;

    @Override
    protected SubjectRelationTypeEnum getRelationType() {

        return SubjectRelationTypeEnum.USER_SUBSCRIBE_SUBJECT;
    }

    @Override
    protected boolean isAsyncDoAfterComplete() {

        return true;
    }
    
    @Override
    protected void doAfterComplete(Long id, String userId, Long subjectId, boolean subscribe) {
        if (subscribe) {
            super.init(userId, subjectId);
            super.addTargetAccountRelationListCache(userId, subjectId);
            super.init(userId, subjectId);
            super.addTargetAccountRelationListCache(userId, subjectId);
            super.addTargetAccount2SourceLastSubscribeList(userId, subjectId);
            // 推送被订阅用户消息红点提醒
            this.doNotifyRedPointByWebSocket(userId);
        }
    }
    
    /**
     * 基于ws，为targetAccount推送订阅消息红点提醒
     *
     */
    private void doNotifyRedPointByWebSocket(String userId) {
        MessageNotifyVO messageBase = new MessageNotifyVO();
        messageBase.setReceiverId(String.valueOf(userId));
        messageBase.setType(MessageTypeEnum.SUBSCRIBE_SUBJECT.getType());
        messageQueueProducer.broadSend(MessageBroadChannelEnum.QUEUE_CHAT_NOTIFY, messageBase);
    }
    
    
}
